﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Threading;

namespace DIST.LR.ZHJG.Controls.Chart
{
    public partial class ucGagueNew : DynamicBasePage
    {
        ucGagueNewViewModel viewmodel;
        public ucGagueNew()
        {
            InitializeComponent();
            viewmodel = new ucGagueNewViewModel()
            {
                Width = 200,
                Height = 100,
                Min = 0,
                Max = 240,
                MajorTickStep = 20,
                Value = 180
            };
            this.DataContext = viewmodel;
        }

        protected override void NewValue()
        {
            //needleNorth.Value = 180;
        }
    }

    public partial class DynamicBasePage : UserControl
    {
        /// <summary>
        /// Timer interval
        /// </summary>
        protected TimeSpan interval = TimeSpan.FromSeconds(1);

        /// <summary>
        /// Timer to change indicator's value
        /// </summary>
        protected DispatcherTimer timer;

        /// <summary>
        /// Randomizer
        /// </summary>
        protected Random rnd = new Random();

        public DynamicBasePage()
        {
            this.Loaded += new RoutedEventHandler(DynamicBasePage_Loaded);
            this.Unloaded += new RoutedEventHandler(DynamicBasePage_Unloaded);
        }

        private void DynamicBasePage_Loaded(object sender, RoutedEventArgs e)
        {
            timer = new DispatcherTimer();
            timer.Interval = interval;
            timer.Tick += new EventHandler(Timer_Tick);
            timer.Start();
        }

        private void DynamicBasePage_Unloaded(object sender, RoutedEventArgs e)
        {
            if (timer != null)
            {
                timer.Stop();
                timer = null;
            }
        }

        /// <summary>
        /// Looks for the object with given name
        /// through the gauge objects. This method
        /// should be overriden in derivation classes.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        protected virtual object FindInGauge(string name)
        {
            return null;
        }

        /// <summary>
        /// Gets or sets timer interval
        /// </summary>
        protected TimeSpan TimerInterval
        {
            get
            {
                return interval;
            }

            set
            {
                timer.Stop();

                interval = value;
                timer.Interval = interval;

                timer.Start();
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (timer != null)
            {
                timer.Stop();

                NewValue();

                timer.Start();
            }
        }

        protected virtual void NewValue()
        {
        }

        /// <summary>
        /// Setup binding between GUI element (like TextBox or ComboBox)
        /// and gauge element
        /// </summary>
        /// <param name="guiElement"></param>
        /// <param name="valueProperty"></param>
        /// <param name="gaugeElement"></param>
        /// <param name="gaugeProperty"></param>
        protected void SetupBinding(FrameworkElement guiElement,
            DependencyProperty valueProperty,
            FrameworkElement gaugeElement,
            DependencyProperty gaugeProperty,
            IValueConverter converter)
        {
            SetupBinding(guiElement, valueProperty, gaugeElement, gaugeProperty, converter, null);
        }

        /// <summary>
        /// Setup binding between GUI element (like TextBox or ComboBox)
        /// and gauge element
        /// </summary>
        /// <param name="guiElement"></param>
        /// <param name="valueProperty"></param>
        /// <param name="gaugeElement"></param>
        /// <param name="gaugeProperty"></param>
        protected void SetupBinding(FrameworkElement guiElement,
            DependencyProperty valueProperty,
            FrameworkElement gaugeElement,
            DependencyProperty gaugeProperty,
            IValueConverter converter,
            object converterParameter)
        {
            BindingHelper helper = new BindingHelper();
            Binding binding = new Binding();
            binding.Source = helper;
            binding.Path = new PropertyPath("Value");
            binding.Mode = BindingMode.TwoWay;
            binding.Converter = converter;
            binding.ConverterParameter = converterParameter;
            guiElement.SetBinding(valueProperty, binding);

            binding = new Binding();
            binding.Source = helper;
            binding.Path = new PropertyPath("Value");
            gaugeElement.SetBinding(gaugeProperty, binding);
        }
    }

    /// <summary>
    /// This class facilitates databinding by providing a property with change notification.
    /// </summary>
    public class BindingHelper : INotifyPropertyChanged
    {
        private object originalValue;

        public event PropertyChangedEventHandler PropertyChanged;

        public object Value
        {
            get
            {
                return this.originalValue;
            }

            set
            {
                this.originalValue = value;
                this.OnPropertyChanged("Value");
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
